package com.yw.atp.util;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class PushUtil {
	private static final String DEFAULT_CHARSET = "utf8";
	private static final int timeoutConnection = 3000;  
	private static final int timeoutSocket = 5000; 
	public static String push(String url,boolean isGet,Map<String,Object> params) throws Exception{
		HttpClient client = new DefaultHttpClient();
		HttpParams hp = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(hp, timeoutConnection);  
		HttpConnectionParams.setSoTimeout(hp, timeoutSocket); 
		HttpResponse response;
		if(isGet){
			if(params!=null){
				Set<String> keys = params.keySet();
				StringBuffer parStr = new StringBuffer();
				String b = "?";
				for(String key:keys){
					parStr.append(b+URLEncoder.encode(key, DEFAULT_CHARSET)+"="+URLEncoder.encode(params.get(key).toString(),DEFAULT_CHARSET));
					b = "&";
				}
				url += parStr.toString();
				System.out.println(url);
			}
			HttpGet get  = new HttpGet(url);
			get.setParams(hp);
			response = client.execute(get);
		}else{
			HttpPost post = new HttpPost(url);
			post.setParams(hp);
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
			//需要通过POST提交的参数  
			if(params!=null){
				Set<String> keys = params.keySet();
				for(String key:keys){
					nvps.add(new BasicNameValuePair(key,params.get(key).toString()));  
				}
			}
			post.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));  
			response = client.execute(post);
		}
		
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			HttpEntity entity = response.getEntity();
			String s = EntityUtils.toString(entity, DEFAULT_CHARSET);
			System.out.println(s);
			return s;
		}
		return null;
	}
	
	public static void polling(String mobileID,String appId,String content,boolean isGet) throws Exception{
		Map<String,Object> parms = new HashMap<String, Object>();
		parms.put("action","handleRequest");
		parms.put("phoneNum",mobileID);
		parms.put("appId",appId);
		parms.put("message",content);
		System.out.println("mobileID : "+mobileID);
		System.out.println("appId : "+appId);
		System.out.println("content : "+content);
		PushUtil.push(Constants.PUSH_URL,isGet,parms);
	}
	
	public static void main(String[] args) throws Exception {
		Map<String,Object> parms = new HashMap<String, Object>();
		parms.put("action","handleRequest");
		parms.put("phoneNum","all");
		parms.put("appId","1");
		parms.put("message","33333333333");
		push("http://61.172.246.7:7070/push.do",false,parms);		
		
//		Map<String,Object> parms = new HashMap<String, Object>();
//		parms.put("command.title","测试HTTP文件上传");
//		parms.put("command.content","测试HTTP文件上传");
//		parms.put("imgFile",new File("d:/test.jpg"));
//		parms.put("widthFile","1");
//		parms.put("command.commType","1");
//		parms.put("command.createdAt","2010-10-10 22:22:22");
//		upload("http://61.172.246.7/Atp/command/add.action",parms);
	}
}
